/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2008 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: main.cpp 205 2012-07-17 12:42:23Z jlauha $
 * $Revision: 205 $
 */

#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // _MSC_VER

#include <assert.h>

#include "irrlicht.h"
#include "main.h"

using namespace irr;

bool gAlive = true;

int gVersion = 0x000100;
char *gVersionString = "0.1.0";


#ifdef _MSC_VER
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char* argv[])
#endif
{
    video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
    //video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;

    IrrlichtDevice *device =
        createDevice(driverType, turska::GraphicsIrr::irrdimension2d(640, 480), 32, false, true);

    if (device == 0)
        return 1; // could not create selected driver.

    device->setWindowCaption(L"Loading...");

    video::IVideoDriver *driver = device->getVideoDriver();
    scene::ISceneManager *smgr = device->getSceneManager();

    // Display possible splash screen here and then load data

    App *app = new App(device);
    assert(app);
    if (app != NULL)
    {
        bool result = app->init(device);
        if (result)
        {
            device->setEventReceiver(app);

            // Create a state machine for different app states
            // if you create more of them...

            MainState *ms = new MainState;
            if (ms && ms->init())
            {
                app->setAppStateHandler(ms);
                app->run();
            }
            else
                gErrorMessage("Error", "Couldn't initialize MainState.");
            delete ms;
        }
        delete app;
    }

    device->drop();

    return 0;
}
